<?php

/* TODO: Add code here */
/**
 * Lớp đối tượng của FAQ phụ trách tương tác với cơ sở dữ liệu và những phương thức khác liên quan.
 * 
 **/
class user_catalogue
{
	#region user_common
	const ACT_ADD							= 10;
	const ACT_UPDATE						= 11;
	const ACT_DELETE						= 12;
	const ACT_CHANGE_PAGE					= 13;
	const ACT_SHOW_EDIT = 14;
	const ACT_GET = 15;
	const NUM_PER_PAGE = 10;
	
	const TBL_T_CAT				= 't_catalogue';
	
	const SQL_INSERT_CAT			= 'INSERT INTO `{0}`
		(`cat_id`,
		`name`,
		`detail`,
		`section_id`,
		`tree_function`,`tree_html`)
		VALUES (\'{1}\',\'{2}\',\'{3}\',\'{4}\',\'{5}\',\'{6}\');';
	const SQL_UPDATE_CAT			= 'UPDATE `{0}`
											SET 
											`name` = \'{1}\',
											`detail` = \'{2}\',
											`section_id` =  \'{3}\',
		`tree_function` =  \'{4}\',
		`tree_html` =  \'{5}\'
											WHERE `cat_id` = \'{6}\';';
	
	const SQL_CREATE_TABLE_T_CAT		= 'CREATE TABLE `{0}` (
											  `cat_id` bigint(20) NOT NULL COMMENT \'Mã nhà sản xuất tăng tự động\',
											  `name` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT \'Tên nhà sản xuất\',
											  `detail` text COLLATE utf8_unicode_ci COMMENT \'Thông tin thêm về nhà sản xuất\',
											  `delete_flag` tinyint(4) DEFAULT NULL COMMENT \'0: avaiable,1 : delete\',
											  `section_id` bigint(20) DEFAULT NULL,
												`tree_function` text COLLATE utf8_unicode_ci COMMENT \'chứa nội dung cấu trúc chức năng\',
												`tree_html` text COLLATE utf8_unicode_ci COMMENT \'chứa nội dung cấu trúc chức năng\',
											  PRIMARY KEY (`cat_id`)
											) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
	#end region
	
	
	#region Variables
	var $_objConnection;
	#end region
	
	
	#region Contructors
	/**
	*  Phương thức khởi tạo đối tượng faq đồng thời tạo connection đến db
	*
	* @param object $objConnection Đối tượng kết nối đến db
			
	* @return void 
	*
	*/
	public function user_catalogue($objConnection)
	{
		$this->_objConnection = $objConnection;
		
	}
	#end region
	
	
	#region Private Functions
	
	#end region
	
	
	#region Public Functions
	
	
	public function insert($name,$detail,$section_id,$tree_function,$m_htmlTree )
	{
		$intID = user_common::getMaxID(self::TBL_T_CAT);
		$strSQL = user_common::prepareQuery(self::SQL_INSERT_CAT,
				array(self::TBL_T_CAT,$intID,user_common::escape_mysql_string($name),user_common::escape_mysql_string($detail),$section_id ,user_common::escape_mysql_string($tree_function),user_common::escape_mysql_string($m_htmlTree)));
		
		if (!user_common::ExecutequeryWithCheckExistedTable($strSQL,self::SQL_CREATE_TABLE_T_CAT,$this->_objConnection,$strTableName))
		{
			//echo $strSQL;
			user_common::writeLog('Error add catalogue:'.$strSQL,1);
			return false;
		}	
		return $intID;
		
	}
	public function update($intID,$name,$detail,$section_id,$tree_function,$m_htmlTree)
	{
		$strSQL = user_common::prepareQuery(self::SQL_UPDATE_CAT,
				array(self::TBL_T_CAT,user_common::escape_mysql_string($name),user_common::escape_mysql_string($detail),$section_id,user_common::escape_mysql_string($tree_function),user_common::escape_mysql_string($m_htmlTree),$intID ));
		
		if (!user_common::ExecutequeryWithCheckExistedTable($strSQL,self::SQL_CREATE_TABLE_T_CAT,$this->_objConnection,$strTableName))
		{
			//echo $strSQL;
			user_common::writeLog('Error add catalogue:'.$strSQL,1);
			return false;
		}	
		return $intID;
		
	}
	/**
	 * Hàm lấy tất cả record của c_doc_xxxx dựa vào danh sách doc_id truyền vào dùng cho việc search.
	 *	
	 * @param string $selectField Cho biết các field cần lấy.
	 * @return array Danh sách record 
	 * @author ThanhViet added [20101005]
	 */
	public function getAllCatBySection($sect_id,$selectField='*') 
	{
		/*$arrDocID = Array();
		foreach($listID as $item)
		{
			$arrDocID[$item['id']] = $item['id'];
		}*/
		//echo '<br>count:'.count($listID);
	
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, array($selectField, self::TBL_T_CAT ,' where section_id='.$sect_id.' order by name'));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult)
		{
			user_common::writeLog('getAllCatBySection:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		//print_r($arrResult);
		return $arrResult;
	}
	
	/**
	 * Hàm lấy tất cả record của c_doc_xxxx dựa vào danh sách doc_id truyền vào dùng cho việc search.
	 *	
	 * @param array $objID mang chua doc_id va weight.
	 * @param string $selectField Cho biết các field cần lấy.
	 * @return array Danh sách record 
	 * @author ThanhViet added [20101005]
	 */
	public function getCatByID($objID,$selectField='*') 
	{
		
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, 
				array($selectField, self::TBL_T_CAT ,							
					'WHERE cat_id = \''.$objID.'\' '));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult)
		{
			user_common::writeLog('getCatByID:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		
		
		//print_r($arrResult);
		return $arrResult[0];
	}
	
	/**
	 * Hàm lấy tất cả record của c_doc_xxxx dựa vào danh sách doc_id truyền vào dùng cho việc search.
	 *	
	 * @param array $listID mang chua doc_id va weight.
	 * @param string $selectField Cho biết các field cần lấy.
	 * @return array Danh sách record 
	 * @author ThanhViet added [20101005]
	 */
	public function getCatByIDs($listID,$selectField='*') 
	{
		/*$arrDocID = Array();
		foreach($listID as $item)
		{
			$arrDocID[$item['id']] = $item['id'];
		}*/
		//echo '<br>count:'.count($listID);
		$strID='';
		foreach ($listID as $key)
		{
			$strID.='\''.$key['cat_id'].'\',';
		}
		$strID = user_common::cutLast($strID,1);
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, 
				array($selectField, self::TBL_T_CAT ,							
					'WHERE cat_id IN ('.$strID.')'));
		
		
		
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult)
		{
			user_common::writeLog('getCatByIDs:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		
		
		//print_r($arrResult);
		return $arrResult;
	}
	
	
	
	/**
	 * This is method outputHTMLSection select option
	 *
	 * @param mixed $arrFaq This is a description
	 * @param mixed $strKeyword This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function outputHTMLCatalogue($arrCat)
	{
		$strHTML= '<select id="txtCat" name="txtCat" value="" style="width: 50%;">';
		$strHTML .= '<option value="0">--Chọn nhóm--</option>';
		foreach($arrCat as $item)
		{
			$strHTML .= '<option value="'.$item['cat_id'].'">'.$item['name'].'</option>';
		}
		$strHTML .= '</select>';
		return $strHTML;
	}
	
	public function getListCatalogue($intPage, $where)
	{
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE,array('*',
					self::TBL_T_CAT,' WHERE delete_flag IS NULL or delete_flag=\'0\' ORDER BY `name` ASC  limit '.(($intPage-1)* self::NUM_PER_PAGE).','.self::NUM_PER_PAGE));
		
		$arrCatalogue = $this->_objConnection->selectCommand($strSQL);
		
		$objSection =new user_section($this->_objConnection);
		$arrSection = $objSection->getAllSection();
		//print_r($arrSection);
		if ($arrSection!=null)
		{
			foreach ($arrSection as $item)
			{
				$arrSectionByKey[$item['section_id']] = $item;
			}
		}
		//print_r($arrCat);
		//print_r($arrSectionByKey);
		//chuyển kết quả vào danh sách ID đã theo thứ tự
		$icount = count($arrCatalogue);
		for($i=0;$i<$icount;$i++)
		{
			//echo $arrCatalogue[$i]['section_id'];
			$arrCatalogue[$i]['section_name'] = $arrSectionByKey[$arrCatalogue[$i]['section_id']]['name'];
			//echo $arrSectionByKey[$arrCatalogue[$i]['section_id']]['name'];
		}
		//print_r($arrCatalogue);
		$strHTML = ' <table class="tbl-list">
				<thead>
				<td>Tên</td>
				<td>Mô tả</td>
				<td>Ngành hàng</td>
				<td style="width:100px">Danh mục chức năng</td>
				<td>Display</td>
				<td class="lastCell"></td>
				</thead>
				<tbody>';
	
		for($i=0;$i<$icount;$i++)
		{
			$strHTML.='<tr class="'.($i%2==0?'even':'odd').'">
					<td>'.$arrCatalogue[$i]['name'].'</td>
					<td>'.$arrCatalogue[$i]['detail'].'</td>
					<td>'.$arrCatalogue[$i]['section_name'].'</td>
					<td>'.($arrCatalogue[$i]['tree_function']?'<a href="view_tree.php?type=2&product='.urlencode($arrCatalogue[$i]['name']) .'&script='.urlencode($arrCatalogue[$i]['tree_function']).'" target="_blank" >View Tree</a>':'').'</td>
					<td><input type="checkbox" onclick="objArticleMn.showHideArticle(this)" '.($arrCatalogue['delete_flag']?'':'checked=checked').' /></td>
					<td class="lastCell"><b><a href="javascript:_objCatalogue.showEdit(\''.$arrCatalogue[$i]['cat_id'].'\',\''.$arrCatalogue[$i]['section_id'].'\',this);">Edit</a></b></td>
					</tr>';
		}
		$strHTML.='</tbody></table>';
		
		$strHTML .= "<div>".user_common::getPagingHTMLByNum($intPage,self::NUM_PER_PAGE,user_common::getTotalRecord(self::TBL_T_CAT,$this->_objConnection),
				"_objCatalogue.changePage")."</div>";
		return $strHTML;
	}
	
	#end region
}

?>